<?php
namespace app\cls\OAuth2;
class WeixinThirdLogin extends BaseThirdLogin{
	const TOKEN_URL = "https://api.weixin.qq.com/sns/oauth2/access_token";//获取access_token
	const REFRESH_TOKEN = "https://api.weixin.qq.com/sns/oauth2/refresh_token";//刷新token所使用的url
	const USERINFO_URL = "https://api.weixin.qq.com/sns/userinfo";//获取用户信息的url

    /**
     * @param array $params ["code"=>""]
     * @return mixed
     * @throws AuthException
     */
	public function getAccessToken(array $params=[])
	{
        if(!isset($params["code"])){
            throw new AuthException("缺少必要参数code");
        }
		$data = [
			"appid"=>trim($this->app_key),
			"secret"=>trim($this->app_secret),
			"grant_type"=>"authorization_code",
			];
		foreach($params as $key=>$value){
            $data[$key] = $value;
		}
		$result = $this->curl(self::TOKEN_URL,$data,"get");
		$newData = json_decode($result,true);
		unset($result);
		if(isset($newData["errcode"])){
            throw new AuthException("微信异常1:【{$newData["errcode"]}】:【{$newData["errmsg"]}】");
		}else{
			$result["access_token"] = $newData["access_token"];
			$result["expires_in"] = $newData["expires_in"]-50;//微信减去50秒的过期时间
			$result["refresh_token"] = $newData["refresh_token"];
            $result["refresh_expires_in"] = 6*24*3600+23*3600;//刷新token减去一个小时的过期时间
			$result["uuid"] = $newData["openid"];
			return $result;
		}
	}

	public function refreshAccessToken(array $params=[])
	{
		if(!isset($params["refresh_token"])){
			throw new AuthException("缺少必要参数refresh_token");
		}
		$data = [
		"grant_type"=>"refresh_token",
		"appid"=>$this->app_key,
		"refresh_token"=>$params["refresh_token"]
		];
		$result = $this->curl(self::REFRESH_TOKEN,$data,"get");
		$newData = json_decode($result,true);
		unset($result);
		if(isset($newData["errcode"])){
            throw new AuthException("微信异常:【{$newData["errcode"]}】:【{$newData["errmsg"]}】");
        }else{
			$result["access_token"] = $newData["access_token"];
			$result["expires_in"] = $newData["expires_in"]-50;
			$result["refresh_token"] = $newData["refresh_token"];//刷新token之后，新的刷新token
			return $result;
		}
	}

    /**
     * @param array $params ["access_token"=>"","uuid"=>"uuid"]
     * @return array|mixed
     * @throws AuthException
     */
	public function getUserInfo(array $params=[])
	{
		//参数uid 或者screen_name必须选择其一
		if(!isset($params["access_token"])){
			throw new AuthException("缺少必要参数access_token");
		}
        $data["access_token"] = $params["access_token"];
		if(!isset($params["uuid"])){
			throw new AuthException("缺少必要参数uuid,用户唯一标识符号");
		} 
		$data["openid"] = $params["uuid"];
		$data["lang"] = "zh_CN";
		$result = $this->curl(self::USERINFO_URL,$data,"get");
		//处理返回数据
		$newData = json_decode($result,true);
		unset($result);
		if(isset($newData["errcode"])){
            throw new AuthException("微信异常2:【{$newData["errcode"]}】:【{$newData["errmsg"]}】");
        }else{
			$arr = [];
			$arr["uuid"] = $newData["openid"];
			$arr["nickname"] = $newData["nickname"];
			$arr["head_image"] = $newData["headimgurl"];
			$arr["sex"] = empty($newData["sex"])?3:intval($newData["sex"]);;
			$arr["login_source"] = "weixin";
			return $arr;
		}
	}
}